package com.ff.xyh.service.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.complaint.ComplaintInfo;
import com.ff.xyh.common.entity.post.PostDelete;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.servicebase.exception.MyException;
import com.ff.xyh.common.qo.post.PostShopQuery;
import com.ff.xyh.common.entity.post.PostShop;
import com.ff.xyh.service.post.mapper.PostShopMapper;
import com.ff.xyh.service.post.service.PostShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 拼购表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Service
public class PostShopServiceImpl extends ServiceImpl<PostShopMapper, PostShop> implements PostShopService {

    @Override
    public ResultObject getPostShopPageCondition(PostShopQuery postShopQuery) {
        Page<PostShop> page = new Page<>(postShopQuery.getCurrentPage(), postShopQuery.getPageSize());
        List<PostShop> resultList = new ArrayList<>();
        QueryWrapper<PostShop> wrapper = new QueryWrapper<>();

        // 如果通过id查询 那就只需要查询id 然后直接封装返回
        if (!StringUtils.isEmpty(postShopQuery.getId())) {
            PostShop getById = this.getById(postShopQuery.getId());
            if (getById != null)
                resultList.add(getById);
            return ResultObject.ok().putList(resultList, resultList.size());
        }
        // 不为空 拼接上
        if (!StringUtils.isEmpty(postShopQuery.getContains())) {
            wrapper.and(w ->
                    w.like("title", postShopQuery.getContains())
                        .or().like("content",postShopQuery.getContains()));
        }


        if (postShopQuery.getPlatformId() != null) {
            wrapper.eq("platform_id", postShopQuery.getPlatformId());
        }
        if (postShopQuery.getPostStatus() != null) {
            wrapper.eq("post_status", postShopQuery.getPostStatus());
        }

        if (postShopQuery.getCheckStatus() != null) {
            wrapper.eq("check_status", postShopQuery.getCheckStatus());
        }

        if (postShopQuery.getMaxPrice() != null) {
            wrapper.apply("least_once*price < {0}", postShopQuery.getMaxPrice());
        }
        if (postShopQuery.getMinPrice() != null) {
            wrapper.apply("least_once*price > {0}", postShopQuery.getMinPrice());
        }




        if (postShopQuery.getGmtCreatedAfter() != null) {
            wrapper.ge("gmt_created", postShopQuery.getGmtCreatedAfter());
        }
        if (postShopQuery.getGmtCreatedBefore() != null) {
            wrapper.le("gmt_created", postShopQuery.getGmtCreatedBefore());
        }


        // 按修改时间排序意味着要最新修改
        if (postShopQuery.getOrderByModifiedTime() != null && postShopQuery.getOrderByModifiedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_modified");
        } else {
            wrapper.orderByAsc("gmt_modified");
        }

        // 按创建时间排序意味着要最新
        if (postShopQuery.getOrderByCreatedTime() != null && postShopQuery.getOrderByCreatedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_created");
        } else {
            wrapper.orderByAsc("gmt_created");
        }

        IPage<PostShop> resultPage = this.page(page, wrapper);


        return ResultObject.ok().putList(resultPage.getRecords(), resultPage.getTotal());
    }

    @Override
    public PostShop insertPostShop(PostShop postShop, int level) {
        isArgumentsIllegal(postShop, level);
        if (baseMapper.insert(postShop) > 0)
            return postShop;
        else
            return null;
    }

    private static void isArgumentsIllegal(PostShop postShop, int level) {
        // 单位 分
        int costLevel = 10000;
        int costLevel1 = 20000;
        int costLevel2 = 50000;
        int costLevel3 = 100000;
        if ((postShop.getTotal() - postShop.getUserNumber()) < postShop.getLeastOnce())
            throw new MyException(20001, ResultMessage.ERROR_ARG);
        // 消费等级
        switch (level) {
            case 1 :
                costLevel = costLevel1;
                break;
            case 2 :
                costLevel = costLevel2;
                break;
            case 3 :
                costLevel = costLevel3;
                break;
            default:
                break;

        }
        if (postShop.getPrice() * postShop.getLeastOnce() > costLevel)
            throw new MyException(20001, "超出限定金额: " + costLevel );
    }

}
